/*
 * Copyright 2021-2030 itbox.cn All right reserved. This software is the confidential and proprietary information
 * of itbox.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered into with itbox.cn.
 */
package cn.itbox.example.application.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.itbox.example.application.service.UserQueryService;
import cn.itbox.example.model.response.UserResponse;
import cn.itbox.example.model.request.UserPageRequest;
import cn.itbox.example.domain.model.UserModel;
import cn.itbox.example.domain.repository.UserRepository;
import cn.itbox.uap.db.model.PageResult;
import cn.itbox.uap.domain.util.Converter;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongProject springboot-study
 * @BelongPackage cn.itbox.example.application.service.impl
 * @Description TODO
 * @ClassName UserQueryServiceImpl
 * @Author xingc
 * @Date 2023/11/27 10:05
 * @Version 1.0.0
 */
@Slf4j
@Service
public class UserQueryServiceImpl implements UserQueryService {
    @Resource
    private UserRepository userRepository;

    @Override
    public UserResponse byId(Long id) {
        UserModel model = userRepository.byId(id);
        return BeanUtil.copyProperties(model, UserResponse.class);
    }

    @Override
    public List<UserResponse> list() {
        List<UserModel> modelList = userRepository.findAll();
        List<UserResponse> dtoList = modelList.stream().map(item -> BeanUtil.copyProperties(item, UserResponse.class)).collect(Collectors.toList());
        return dtoList;
    }

    @Override
    public PageResult<UserResponse> pageList(Long pageIndex, Long pageSize) {
        PageResult<UserModel> modelList = userRepository.findPage(pageIndex, pageSize);
        return Converter.model2Dto(modelList, UserResponse.class);
    }

    @Override
    public PageResult<UserResponse> pageList(UserPageRequest request) {
        PageResult<UserModel> modelList = userRepository.findPage(request.getUserName(), request.getPage(), request.getPageSize());
        return Converter.model2Dto(modelList, UserResponse.class);
    }
}
